package com.gsq.learning.mybatisplugin.plugin.interceptor;

import com.gsq.learning.mybatisplugin.domain.User;
import org.apache.ibatis.executor.resultset.ResultSetHandler;
import org.apache.ibatis.plugin.*;

import java.sql.Statement;
import java.util.List;
import java.util.Properties;

@Intercepts({
        @Signature(
                type = ResultSetHandler.class,
                method = "handleResultSets",
                args = {Statement.class}
        )
})
public class MyResultSetInterceptor implements Interceptor {
    @Override
    public Object intercept(Invocation invocation) throws Throwable {

        ResultSetHandler target = (ResultSetHandler) invocation.getTarget();
        System.out.println("======> MyResultSetInterceptor ：" + target);

        Object proceed = invocation.proceed();

        if (proceed instanceof List) {
            if (((List) proceed).size() > 0) {
                Object o = ((List) proceed).get(0);
                if (o instanceof User) {
                    User user = (User) o;
                    user.setBirthday("2000-01-01"); // 修改list第一个元素的birthday值
                }
            }
        }
        return proceed;
    }

    @Override
    public Object plugin(Object target) {
        return Plugin.wrap(target, this);
    }

    @Override
    public void setProperties(Properties properties) {

    }
}
